package com.ncd.form.controller;

import com.ncd.common.annotation.Log;
import com.ncd.common.core.controller.BaseController;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.core.page.TableDataInfo;
import com.ncd.common.core.text.Convert;
import com.ncd.common.enums.BusinessType;
import com.ncd.form.domain.Layout;
import com.ncd.form.service.ILayoutService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单名称Controller
 *
 * @author tree_new_bee
 */
@Controller
@RequestMapping("/form/layout")
public class LayoutController extends BaseController {

    private String prefix = "form/layout";

    @Resource
    private ILayoutService layoutService;

    @RequiresPermissions("form:layout:view")
    @GetMapping()
    public String layout()
    {
        return prefix + "/layout";
    }

    /**
     * 查询表单名称列表
     */
    @RequiresPermissions("form:layout:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Layout layout)
    {
        startPage();
        List<Layout> list = layoutService.queryList(layout);
        return getDataTable(list);
    }


    /**
     * 新增表单名称
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存表单名称
     */
    @RequiresPermissions("form:layout:add")
    @Log(title = "表单名称", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Layout layout)
    {
        return toAjax(layoutService.save(layout));
    }

    /**
     * 修改表单名称
     */
    @GetMapping("/edit/{layoutId}")
    public String edit(@PathVariable("layoutId") Long layoutId, ModelMap mmap)
    {
        Layout layout = layoutService.getById(layoutId);
        mmap.put("layout", layout);
        return prefix + "/edit";
    }

    /**
     * 修改保存表单名称
     */
    @RequiresPermissions("form:layout:edit")
    @Log(title = "表单名称", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Layout layout)
    {
        return toAjax(layoutService.updateById(layout));
    }

    /**
     * 删除表单名称
     */
    @RequiresPermissions("form:layout:remove")
    @Log(title = "表单名称", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(layoutService.removeByIds(Convert.toLongList(ids)));
    }




    /**
     * 查询字典详细
     */
    @RequiresPermissions("form:layout:list")
    @GetMapping("/detail/{layoutId}")
    public String detail(@PathVariable("layoutId") Long layoutId, ModelMap mmap)
    {
        mmap.put("layoutId", layoutId);
        return "form/version/version";
    }


    /**
     * 进入复制表单
     */
    @GetMapping("/copy/{layoutId}")
    public String copy(@PathVariable("layoutId") Long layoutId, ModelMap mmap)
    {
        mmap.put("layoutId", layoutId);
        return prefix + "/copy";
    }

    /**
     * 复制表单
     */
    @PostMapping("/copySave")
    @ResponseBody
    public AjaxResult copySave(Layout layout)
    {
        return toAjax(layoutService.copyLayout(layout));
    }



}
